<?php

namespace App\Http\Controllers\houtai;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cookie;
use DB;
use App\Survey;
use App\seller;

use App\manager;
use App\appraiser;
use App\Car;


class AdminController extends Controller
{

        /**
        * 后台登陆页面
        */
        public function admin()
        {
            //判断cookie有没有存了name
            $cook = Cookie::get('name');
            //如果为真,那就直接进入后台
            if ($cook) {
                Cookie::forget('name');
                return view('admin');
            } else {
            //如果为假,那就跳转到登陆页面
            return view('dengru');
            }
        }

        /**
         * 评估师详细车辆列表页
         */
        public function carDetailList()
        {
            $carList = Car::
                    where(['survey_status'=>0])
                    ->select('car_id','seller_intro')
                    ->paginate(10);
            //dd($carList);
            return view('carDetailList',['carList'=>$carList]);
        }

        /**
         * 获取到ID进行修改
         */
         public function carDetail(Request $request, $car_id)
         {
             if (empty($_POST)) {
                return view('carDetail',['car_id'=>$car_id]);
            } else {
                $this->carDetailpost($request);
            }
             //$sur = Survey::where('car_id','=',$car_id)->first();
            //  dd($sur->app_intro);


         }

        /**
         * 评估师提交车辆信息页面
         */
        public function info()
        {
            return view('info');
        }

        /**
         * 评估师填写卖家信息页面
         */
        public function sellerList()
        {
            $seller = DB::table('car')
                   ->where(['seller_status'=>0])
                   ->select('car_id','seller_intro')
                   ->paginate(10);
                //    dd($seller);
            return view('sellerinfoList',['seller'=>$seller]);
        }

        /**
         * 后台登陆页面
         */
        public function dengru(){
            return view('dengru');
        }

        //成功界面展示
        public function educty(){
            return view('educt');
        }
        //失败页面展示
        public function eductn(){
            return view('eductn');
        }
    //ajax 验证
    public function yanzhengpost(Request $req){
            //查找评估师表单是否有登入账号密码
            $password = md5($req->password);
            $path = DB::table('appraiser')->where(["name"=>$req->name,"password"=>$password])->first();
            Cookie::queue('name',$req->name,30);

            if ($path) {
                return 1;
            } else {
                return 0;
            }
    }


//评估师审核信息
    public function carInfoList()
    {
        $check = DB::table('car')
            ->where(['check_status'=>0])
            ->select('car_id','brand_name','series_name','mileage')
            ->paginate(10);
        // dd($check->all());
        return view('carInfoList',['check'=>$check]);
    }

    //评估师审核详情页
    public function rewrite(Request $req)
    {
        $car = DB::table('car')->where('car_id',$req->car_id)->first();
        return view('rewrite',['c'=>$car]);
    }

    public function addCarSeller(Request $req,$car_id)
    {
         if (empty($_POST)) {
              return view('sellerList',['car_id'=>$car_id]);
         } else {
              return $this->sellerpost($req);
         }

    }



    //评估师审核详情修改上传
    public function rewritePost(Request $req)
    {


        $car = Car::where('car_id',$req->car_id)->first();
        if(empty($_POST)){
             $data = "数据不能为空";
            return view('educt',['detail'=>$data]);
        } else {
            $car->price = $req->price;
            $car->brand_name = $req->brand_name;
            $car->reg_time = $req->reg_time;
            $car->mileage = $req->mileage;
            $car->license_addr = $req->license_addr;
            $pros = config('provinces');
            $car->checkcar_pro = $pros[$req->pro];
            $car->checkcar_city = $req->city;
            $car->checkcar_addr = $req->checkcar_addr;
            $car->type = $req->type;
            $car->seller_intro = $req->seller_intro;
            $car->pubtime = time();
            $car->seller_intro = $req->seller_intro;
            $car->year_type = $req->year_type;
            $car->emission = $req->emission;
            $car->series_name = $req->series_name;
            $car->check_status = 1;
            $car->app_id = 1;
            if($car->save()){
                return $this->educty();
            } else {
                return $this->eductn();
            }
        }
    }

    public function carDetailpost(Request $req)
    {

        $por = new Survey();
        //表单信息添加
        if(empty($_POST)) {
                return "数据不能为空";
         } else {
            $name = Cookie::get('name');
            // dd($name);
            $app = DB::table('appraiser')->where('name',$name)->select('app_id')->first();
            // dd($app->app_id);
            $app_id = $app->app_id;
            // dd($app_id);
            $por->app_id = $app_id;
            // dd($pro->app_id);
            $por->car_id = $req->car_id;
            $por->ex_collision = $req->ex_collision;
            $por->ex_water = $req->ex_water;
            $por->ex_fire = $req->ex_fire;
            $por->wg_repair = $req->wg_repair;
            $por->wg_defect = $req->wg_defect;
            $por->in_defect = $req->in_defect;
            $por->xt_light = $req->xt_light;
            $por->xt_engine = $req->xt_engine;
            $por->xt_tool = $req->xt_tool;
            $por->js_start = $req->js_start;
            $por->js_driver = $req->js_driver;
            $por->js_brake = $req->js_brake;
            $por->app_intro = $req->app_intro;
            $arr = $this->Surveytu($por,$req);
            return $arr;

         }
    }

    //取出图片地址
    // public function shouyetu(){
    //
    //    $path = DB::table('survey')->where('survey_id','25')->get();
    //    //var_dump($path);
    //    return view('index',['msgs'=>$path]);
    // }

    //图片添加
     public function Surveytu($por,$req)
     {
        //首页文件名生成.mysql_insert_id()
        $indexImgName = date('Ymd',time()).rand(1000,9999);
        //图片目录public_path().
        $indexImgPath = "/images/imgdex/";
        $otherImgPath = "/images/imgche/";
       if($req->file('index_img_path'))
       {
            $req->file('index_img_path')->move(public_path().$indexImgPath, $indexImgName.'.jpg');
            $por->index_img_path = $indexImgPath.$indexImgName.'.jpg';
       } else {
            return "请添加图片";
       }

        //车辆其他图片存储
        $por->other_img_path = $otherImgPath;
        if($req->hasFile('images'))
        {

            $str = '';
            foreach($req->file('images') as $value)
            {
               $otherImgName = date('Ymd',time()).rand(1000,9999);
               $value->move(public_path().$otherImgPath, $otherImgName.'.jpg');
               $str .= $otherImgName.'.jpg ';
               $por->other_img_name=$str;
            }

        }
        if ($por->save()) {
            DB::table('car')->where('car_id',$por->car_id)->update(['survey_status'=>1]);
            return $this->educty();
        } else {
            return $this->eductn();
        }
     }



    //添加用户信息
    public function sellerpost(Request $req){
        $por = new Seller();
            // dd($req->car_id);
        //数据获取添加
            $car = DB::table('car')->where('car_id',$req->car_id)->select('uid')->first();
            $uid = $car->uid;
            $por->uid          = $uid;
            $por->host         = $req->host;
            $por->job          = $req->job;
            $por->addr         = $req->addr;
            $por->year_check   = $req->year_check;
            $por->strong_insur = $req->strong_insur;
            $por->trade        = $req->trade;
            $por->transfer     = $req->transfer;
            $por->car_id       = $req->car_id;
            if($por->save()){
                DB::table('car')->where('car_id',$por->car_id)->update(['seller_status'=>1]);
                return $this->educty();
            } else {
                return $this->eductn();
            }

        }

    }
